#============================================================
#    Node2D Array
#============================================================
# - datetime: 2022-09-12 23:09:16
#============================================================


##  对队列里的节点进行操作
class_name Node2DArray
extends Node2D


var __data := {}



func size() -> int:
	return __data.size()

func has(target: Node2D) -> bool:
	return __data.has(target)

func add(target: Node2D):
	__data[target] = null

func remove(target: Node2D):
	__data.erase(target)

## 获取第一个节点
func get_first() -> Node2D:
	if __data.size() > 0:
		return __data.keys().front()
	return null

func all() -> Array[Node]:
	return __data.keys()


##  获取最后一个
func get_last() -> Node2D:
	if __data.size() > 0:
		return __data.keys().back()
	return null


##  获取最近节点
func get_closest() -> Node2D:
	if __data.size() > 0:
		if __data.size() == 1:
			return __data.keys()[0]
		# 排序找到最近的节点
		var list : Array[Node2D] = __data.keys()
		list.sort_custom(func(a: Node2D, b: Node2D):
			return a.global_position.distance_squared_to(self.global_position) < b.global_position.distance_squared_to(self.global_position)
		)
		return list.front()
	return null


##  获取范围内的节点
func get_range(distance: float) -> Array[Node2D]:
	var squared_distance := pow(distance, 2)
	var list : Array[Node2D] = __data.keys()
	list.filter(func(node: Node2D):
		return node.global_position.distance_squared_to(self.global_position) <= squared_distance
	)
	return list


